<?php
namespace api\service;

use api\service\interfaces\IOfferService;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\utils\AjaxResult;
use common\service\CommonService;
use herosphp\bean\Beans;

Loader::import('api.service.interfaces.IOfferService');

/**
 * Offer报价 api(Service)接口实现
 * @author yu
 */
class OfferService extends CommonService implements IOfferService
{


    public function __construct()
    {
        $this->thisModel = Loader::model('offer');

    }

    //用户报价数据统计
    public function searchByStatistics($pagesize, $page, $search_arr)
    {
        $offset = $pagesize * ($page - 1); //计算记录偏移量

        $count_sql="Select count(*) As g From (Select saas_user_id From offer GROUP BY saas_user_id) sub_query";

        $total = $this->thisModel->getDB()->excute($count_sql)->fetchColumn();

        //分页查询
        $pageinationSql = isset ($offset) ? " Limit $offset,$pagesize" : " Limit 0,$pagesize";

        $sql="Select
t.saas_user_id,
t.addtime,
count(t.id) As offer_counts ,
sum(t.views) As offer_views
From
  (Select * From offer  Order By addtime Desc)  t
GROUP BY t.saas_user_id Order By t.addtime Desc $pageinationSql";

        $items = $this->thisModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $items, "total" => $total));

    }

    //报价单列表
    public function searchByOffers($pagesize, $page, $search_arr)
    {


        $service = Beans::get('offer.offer.service');

        $result = $service->getOfferList($pagesize,$page,$search_arr);


        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", array("items" => $result['items'], "total" => $result['total']));

    }

    //报价单产品列表
    public function searchByOfferProducts($search_arr)
    {
        $service = Beans::get('offer.offer.service');

        $result['items'] = $service->getOfferProducts($search_arr['offer_id']);
        $result['head'] = $service->getOffer($search_arr['offer_id']);
        Log::info($search_arr);
        Log::info($result);

        return new AjaxResult(AjaxResult::OP_SUCCESS, "loading successfully", $result);

    }
}
